package com.jade.explorer.ui.property;

import java.util.HashMap;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

import com.jade.explorer.XMLpart;
import com.jade.explorer.ui.SWTResourceManager;
import com.jade.explorer.views.JadeSharedImages;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;


public class ProjectProperty extends Dialog {
	private Text nameText;
	private Text authorText;
	private Text companyText;
	private Text dateText;
	private Text descText;
	private String _path;

	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public ProjectProperty(Shell parentShell, String path) {
		super(parentShell);
		_path = path + "\\.project";
	}

	
	private void infoLabel(Composite parent){
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setBounds(0, 43, 444, 206);
		
		
		HashMap<String, String> data = XMLpart.getProjectProperty(_path);
		
		Label nameLabel = new Label(composite, SWT.NONE);
		nameLabel.setBounds(10, 11, 73, 15);
		nameLabel.setText("Name:");
		
		Label authorLabel = new Label(composite, SWT.NONE);
		authorLabel.setBounds(10, 35, 73, 15);
		authorLabel.setText("Author:");
		
		Label companyLabel = new Label(composite, SWT.NONE);
		companyLabel.setBounds(10, 57, 73, 15);
		companyLabel.setText("Company:");
		
		Label dateLabel = new Label(composite, SWT.NONE);
		dateLabel.setBounds(10, 83, 73, 15);
		dateLabel.setText("Created date:");
		
		Label descriptionLabel = new Label(composite, SWT.NONE);
		descriptionLabel.setBounds(10, 110, 73, 15);
		descriptionLabel.setText("Desription:");
		
		nameText = new Text(composite, SWT.BORDER);
		nameText.setEditable(false);
		nameText.setBounds(89, 7, 345, 21);
		nameText.setText(data.get("name"));
		
		authorText = new Text(composite, SWT.BORDER);
		authorText.setEditable(false);
		authorText.setBounds(89, 31, 345, 21);
		authorText.setText(data.get("author"));
		
		companyText = new Text(composite, SWT.BORDER);
		companyText.setEditable(false);
		companyText.setBounds(89, 55, 345, 21);
		companyText.setText(data.get("company"));
		
		dateText = new Text(composite, SWT.BORDER);
		dateText.setEditable(false);
		dateText.setBounds(89, 80, 345, 21);
		dateText.setText(data.get("created"));
		
		descText = new Text(composite, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
		descText.setEditable(false);
		descText.setBounds(89, 107, 345, 89);
		descText.setText(data.get("desc"));
	}
	
	
	private void errLabel(Composite parent){
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setBounds(0, 43, 444, 206);
		composite.setLayout(null);
		
		Label errLabel = new Label(composite, SWT.NONE);
		errLabel.setBounds(133, 85, 195, 15);
		errLabel.setText("Could not read project property file...");
		
		Label errPicLabel = new Label(composite, SWT.NONE);
		errPicLabel.setBounds(111, 85, 16, 16);
		errPicLabel.setImage(JadeSharedImages.getImage(JadeSharedImages.IMG_CROSS));
		
	}
	
	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(null);
		
		//----title
		Composite titleFrame = new Composite(container, SWT.NONE);
		titleFrame.setBounds(0, 0, 444, 44);
		titleFrame.setBackgroundImage(JadeSharedImages.getMenuTitleImg());
		titleFrame.setFont(SWTResourceManager.getFont("Rockwell", 14, SWT.BOLD));
		titleFrame.addPaintListener(new PaintListener() { 
			public void paintControl(PaintEvent e) { 
				e.gc.drawString("Project Property", 10, 10, true); 
				} 
		}); 
		//---
		
		
		if (XMLpart.checkProjectStucture(_path))
			infoLabel(container);
		else
			errLabel(container);
		

		return container;
	}

	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,true);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(450, 328);
	}
}
